package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import io.micrometer.core.instrument.binder.BaseUnits;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author yuxiangxun
 * @date 2024/8/21
 * @apiNote
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 按照条件分页查询护理项目
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        //使用PageHelper插件，设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //分页查询数据，封装到Page<NursingProjectVo>中
        Page<NursingProjectVo> projectVoPage = nursingProjectMapper.getByPage(name, String.valueOf(pageNum), pageSize, status);
        //根据封装的数据，通过NursingProjectVo加载类，在反射过程中将数据映射到与之对应的属性中，然后返回
        return PageResponse.of(projectVoPage,NursingProjectVo.class);
    }

    /**
     * 新增护理项目
     * @param nursingProjectDto
     */
    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        //创建NursingProject对象
        NursingProject nursingProject = new NursingProject();
        //对象拷贝
        BeanUtils.copyProperties(nursingProjectDto, nursingProject);
        //新增护理项目信息
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 回显数据 -> 根据id查询护理项目信息
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        return nursingProjectMapper.getById(id);
    }

    /**
     * 修改护理项目
     * @param nursingProjectDto
     */
    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        //创建NursingProject对象
        NursingProject nursingProject = new NursingProject();
        //对象拷贝
        BeanUtils.copyProperties(nursingProjectDto,nursingProject);
        //更新操作
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 启用-禁用护理项目
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id,status);
    }

    /**
     * 根据id删除护理项目信息
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteById(id);
    }
}
